/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: 7.24client.c
> Author:wyb
> Mail: 
> Created Time: 2024年07月24日 星期三 11时47分41秒
***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include "cJSON.h"
#include "7.23client.h"



//采集水泵信息
struct mywater
{
	int id;	
	char time[50];
	int flow;
	int temperature;
	int liquidlevel;
	int stress;
	char valve[10];
};

//申请连接
int requset_connect();

int main(int argc,const char* argv[])
{
	int num = 0;
	int num1 = 0;
	char type[30] = {0};
	int i = 1;
	int ret1 = 0;
	int value = 0;
	
	struct mywater da;
	memset(&da,0,sizeof(da));
	
	
	
	printf("输入要采集几号水泵：\n");
	scanf("%d",&num);
	if(num == 1)
		strcpy(type,"wph1");
	if(num == 2)
		strcpy(type,"wph2");
	if(num == 3)
		strcpy(type,"wph3");
	

	while(1)
	{
		
		sleep(5);
		da.id = i;
		i++;
		srand((unsigned)time(NULL));
		da.flow = rand()%100;
		da.temperature = rand()%100;
		da.liquidlevel = rand()%4;
		da.stress = rand()%80;
		strcpy(da.valve,"open"); 
		
		//获取当前时间
		time_t now=time(NULL);
		time(&now);
		struct tm *local = localtime(&now);
		sprintf(da.time,"%d-%d-%d-%d-%d-%d", 1900+local->tm_year,1+local->tm_mon,local->tm_mday,local->tm_hour, local->tm_min, local->tm_sec);
		
		//定义cJSON 指针并创建json对象
		cJSON *json = NULL;
		json = cJSON_CreateObject();
		
		//添加多个"键-值”对
		cJSON_AddItemToObject(json,"type",cJSON_CreateString(type));
		cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(da.id));
		cJSON_AddItemToObject(json,"time",cJSON_CreateString(da.time));
		cJSON_AddItemToObject(json,"flow",cJSON_CreateNumber(da.flow));
		cJSON_AddItemToObject(json,"temperature",cJSON_CreateNumber(da.temperature));
		cJSON_AddItemToObject(json,"liquidlevel",cJSON_CreateNumber(da.liquidlevel));
		cJSON_AddItemToObject(json,"stress",cJSON_CreateNumber(da.stress));
		cJSON_AddItemToObject(json,"valve",cJSON_CreateString(da.valve));
		
		//将json对象转成json格式字符串
		char *p = cJSON_Print(json);
			
		//调申请连接函数
		int sock = 0;
		int ret = 0;
		sock = requset_connect();
		
		//发送
		ret = send(sock,p,strlen(p),0);
		if(ret == -1)
		{
			perror("send");
			exit(-1);
		
		}
		
		if(da.liquidlevel < 1 || da.temperature > 85 )
		{
			printf("采集结束\n");
			break;
		}
		
	}
	
    return 0;
}

//申请连接
int requset_connect()
{
	int sock = 0;
	int ret = 0;
	
	//指向sockaddr_in的结构体指针，本地信息地址  绑定IP用的
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
 
 	//创建套接字
 	sock = socket(AF_INET,SOCK_STREAM,0);
 	if(sock == -1)
 	{
 		perror("socket");
 		exit(-1);
 	}
 
 	addr.sin_family = AF_INET;//协议族
 	addr.sin_port = htons(6666);//端口
 	//addr.sin_addr.s_addr = inet_addr("192.168.0.185");//ip
 	addr.sin_addr.s_addr = inet_addr("39.107.122.0");//ip	
 	//与服务器连接，发送连接请求
 	ret = connect(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr));
 	printf("%d",ret);
	if(ret == -1)
	{
		perror("connect");
		exit(-1);
	}
	return sock;
 	
}
